<?php
include_once('baseMysql.php');
date_default_timezone_set('PRC');
/**
 * 实现点赞接口，将点赞数存储在数据库中
 */
class ThumbUp extends BaseMysql {
    private $num;
    private $creator;
    private $thumbUpTime;
    function __construct($servername, $username, $password, $dbname) {
        parent::__construct($servername, $username, $password, $dbname);
        $this->num = $_REQUEST['num'];
        $this->creator = $_REQUEST['creator'] ? $_REQUEST['creator'] : $this->username;
        $this->thumbUpTime = date('Y-m-d H:i:s');
    }

    public function saveThumbUpNum() {
        // 先取出当前操作的人的信息
        $sql = "SELECT `creator`, `num` FROM `thumbup` WHERE `creator` like '$this->creator'";
        $this->result = $this->mysqlQueryArr($sql);
        $arr = array();
        while ($row = mysql_fetch_array($this->result)) {
            array_push($arr, array('creator' => $row['creator'], 'num' => $row['num']));
        }
        if (count($arr)) {
            $currNum = ++$arr[0]['num'];
            $sql = "UPDATE `thumbup` SET `num`='$currNum',`thumbUpTime`='$this->thumbUpTime' WHERE `creator`like'$this->creator'";
        } else {
            $sql = "INSERT INTO `thumbup`(`creator`, `num`, `thumbUpTime`) VALUES ('$this->creator', '$this->num', '$this->thumbUpTime')";
        }
        $this->result = $this->mysqlQueryArr($sql);
        $this->getJsonResult($this->result, '点赞成功！');
    }
}
$thumbup = new ThumbUp('localhost','root','','PHPlesson');
$thumbup->saveThumbUpNum();
?>
